package com.xuexiang.xuidemo.fragment.components.tabbar;

import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentController;
import androidx.fragment.app.FragmentHostCallback;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.jpeng.jptabbar.JPTabBar;
import com.jpeng.jptabbar.OnTabSelectListener;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xui.XUI;
import com.xuexiang.xuidemo.R;
import com.xuexiang.xuidemo.app.activity.TestStartPage;
import com.xuexiang.xuidemo.app.fragment.Functionpage;
import com.xuexiang.xuidemo.app.fragment.Homepage;
import com.xuexiang.xuidemo.app.fragment.Individual;
import com.xuexiang.xuidemo.app.fragment.Tipspace;
import com.xuexiang.xuidemo.base.BaseActivity;
import com.xuexiang.xuidemo.base.BaseFragment;
import com.xuexiang.xuidemo.utils.XToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

import static com.xuexiang.xuidemo.base.BaseActivity.mTitles;


class Myadpter extends FragmentStateAdapter {
    List<Fragment> totalFragment;

    JPTabBar mTabbar;

    public Myadpter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle, List<Fragment> Fragmentresource, JPTabBar mTabbar) {
        super(fragmentManager, lifecycle);
        totalFragment = Fragmentresource;//总体的Fragment
        this.mTabbar = mTabbar;
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        return totalFragment.get(position);//显示的Fragment
    }

    @Override
    public int getItemCount() {
        return totalFragment.size();
    }
}

/**
 * @author xuexiang
 * @since 2019/4/14 下午8:07
 */
@Page(name = "JPTabBar\n一个可以显示中心按钮的TabBar")
public class JPTabBarFragment extends BaseFragment implements OnTabSelectListener {

    @BindView(R.id.view_pager)
    ViewPager2 mViewPager;
    @BindView(R.id.tabbar)
    JPTabBar mTabbar;

    private List<Fragment> mPageMap = new ArrayList<>(); // 关于所有Fragment的总和
//    private Map<String, View> mPageMap = new HashMap<>();

    private Myadpter mPagerAdapter;

    /**
     * 布局的资源id
     *
     * @return
     */
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_jptabbar;
    }

    /**
     * 初始化控件
     * mTabbar的设置详细参见
     * {@link BaseActivity#mTitles}
     * {@link BaseActivity#mSelectIcons}
     * {@link BaseActivity#mNormalIcons}
     */
    @Override
    protected void initViews() {
        mPageMap.add(new Homepage());
        mPageMap.add(new Functionpage());
        mPageMap.add(new Tipspace());
        mPageMap.add(new Individual());
        //页面可以滑动
        mTabbar.setGradientEnable(true);
        mTabbar.setPageAnimateEnable(true);
        mTabbar.setTabTypeFace(XUI.getDefaultTypeface());

        mPagerAdapter = new Myadpter(getActivity().getSupportFragmentManager(), getLifecycle(), mPageMap, mTabbar);

        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                if (mTabbar.getSelectPosition() != position)
                    mTabbar.setSelectTab(position);
            }
        });

        if (mTabbar.getMiddleView() != null) {
            mTabbar.getMiddleView().setOnClickListener(v -> {
                Intent intent = new Intent(getContext(), TestStartPage.class);
                startActivity(intent);
            });
        }

    }

    @Override
    protected void initListeners() {
        mTabbar.setTabListener(this);
    }

    /**
     * 用户每次点击不同的Tab将会触发这个方法
     *
     * @param index 当前选择的TAB的索引值
     */
    @Override
    public void onTabSelect(int index) {
        if (mViewPager.getCurrentItem() != index)
            mViewPager.setCurrentItem(index);
        XToastUtils.toast("点击了" + getString(mTitles[index]));
    }

    /**
     * 这个方法主要用来拦截Tab选中的事件
     * 返回true,tab将不会被选中,onTabSelect也不会被回调
     * 默认返回false
     *
     * @param index 点击选中的tab下标
     * @return 布尔值
     */
    @Override
    public boolean onInterruptSelect(int index) {
        return false;
    }
}
